﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using P3SS.Settings;
using P3SS.Engine;
using P3SS.Networking;

namespace P3SS.ConfigUtil
{
	public partial class frmAccount : Form
	{
		private Account myAccount;
		private bool overrideChecks;

		public Account MyAccount
		{
			get
			{ return myAccount; }
		}

		public frmAccount(Account inMyAccount, bool inOverrideChecks)
		{
			InitializeComponent();
			myAccount = inMyAccount;
			overrideChecks = inOverrideChecks;

			if (inMyAccount != null)
			{
				txtPop3Username.Text = inMyAccount.Pop3Account.Username;
				txtPop3Password.Text = inMyAccount.Pop3Account.Password;
				txtPop3ConfirmPassword.Text = inMyAccount.Pop3Account.Password;
				txtPop3Address.Text = inMyAccount.Pop3Account.Server;
				txtPop3Port.Text = inMyAccount.Pop3Account.Port.ToString();
				chkPop3APOP.Checked = inMyAccount.Pop3Account.UseAPOP;
				chkPop3LeaveMessages.Checked = inMyAccount.Pop3Account.LeaveMailOnServer;
				chkPop3CheckCapabilities.Checked = inMyAccount.Pop3Account.CheckServerCapabilities;

				switch (inMyAccount.Pop3Account.Encryption)
				{
					case EncryptionType.None:
						optPop3EncryptionNone.Checked = true;
						break;
					case EncryptionType.Implicit:
						optPop3EncryptionImplicit.Checked = true;
						break;
					case EncryptionType.Explicit:
						optPop3EncryptionExplicit.Checked = true;
						break;
				}

				switch (inMyAccount.SmtpAccount.Encryption)
				{
					case EncryptionType.None:
						optSmtpEncryptionNone.Checked = true;
						break;
					case EncryptionType.Implicit:
						optSmtpEncryptionImplicit.Checked = true;
						break;
					case EncryptionType.Explicit:
						optSmtpEncryptionExplicit.Checked = true;
						break;
				}

				switch (inMyAccount.SmtpAccount.ToFilter)
				{
					case EmailToFilter.SpecificAddress:
						optSmtpEmailSpecific.Checked = true;
						chkSmtpEmailDomainFilter.Checked = false;
						chkSmtpEmailDomainFilter.Enabled = false;
						txtSmtpEmailAddressSpecific.Text = inMyAccount.SmtpAccount.EmailAddress;
						txtSmtpEmailAddressDomainFilter.Text = "";
						break;
					case EmailToFilter.FilterDomain:
						optSmtpEmailToHeaders.Checked = true;
						chkSmtpEmailDomainFilter.Checked = true;
						chkSmtpEmailDomainFilter.Enabled = true;
						txtSmtpEmailAddressSpecific.Text = "";
						txtSmtpEmailAddressDomainFilter.Text = inMyAccount.SmtpAccount.DomainFilter;
						break;
					case EmailToFilter.AllToHeaders:
						optSmtpEmailToHeaders.Checked = true;
						chkSmtpEmailDomainFilter.Checked = false;
						chkSmtpEmailDomainFilter.Enabled = true;
						txtSmtpEmailAddressSpecific.Text = "";
						txtSmtpEmailAddressDomainFilter.Text = "";
						break;
				}

				txtSmtpUsername.Text = inMyAccount.SmtpAccount.Username;
				txtSmtpPassword.Text = inMyAccount.SmtpAccount.Password;
				txtSmtpConfirmPassword.Text = inMyAccount.SmtpAccount.Password;
				txtSmtpAddress.Text = inMyAccount.SmtpAccount.Server;
				txtSmtpPort.Text = inMyAccount.SmtpAccount.Port.ToString();
			}
		}

		private void validateValues()
		{
			if (!overrideChecks)
			{
				if (string.IsNullOrEmpty(txtPop3Username.Text))
				{
					txtPop3Username.BackColor = Color.Tomato;
				}
				else
				{
					txtPop3Username.BackColor = SystemColors.Window;
				}

				if (string.IsNullOrEmpty(txtPop3Password.Text))
				{
					txtPop3Password.BackColor = Color.Tomato;
				}
				else
				{
					txtPop3Password.BackColor = SystemColors.Window;
				}

				if (string.IsNullOrEmpty(txtPop3Address.Text))
				{
					txtPop3Address.BackColor = Color.Tomato;
				}
				else
				{
					txtPop3Address.BackColor = SystemColors.Window;
				}

				if (string.IsNullOrEmpty(txtSmtpAddress.Text))
				{
					txtSmtpAddress.BackColor = Color.Tomato;
				}
				else
				{
					txtSmtpAddress.BackColor = SystemColors.Window;
				}

				if (optSmtpEmailSpecific.Checked)
				{
					txtSmtpEmailAddressDomainFilter.BackColor = SystemColors.Window;
					if (string.IsNullOrEmpty(txtSmtpEmailAddressSpecific.Text))
					{
						txtSmtpEmailAddressSpecific.BackColor = Color.Tomato;
					}
					else
					{
						txtSmtpEmailAddressSpecific.BackColor = SystemColors.Window;
					}
				}
				else if (optSmtpEmailToHeaders.Checked)
				{
					txtSmtpEmailAddressSpecific.BackColor = SystemColors.Window;
					txtSmtpEmailAddressDomainFilter.BackColor = SystemColors.Window;
					if (chkSmtpEmailDomainFilter.Checked)
					{
						if (string.IsNullOrEmpty(txtSmtpEmailAddressDomainFilter.Text))
						{
							txtSmtpEmailAddressDomainFilter.BackColor = Color.Tomato;
						}
						else
						{
							txtSmtpEmailAddressDomainFilter.BackColor = SystemColors.Window;
						}
					}
				}
			}

			if (txtPop3Password.Text != txtPop3ConfirmPassword.Text)
			{
				txtPop3ConfirmPassword.BackColor = Color.Tomato;
			}
			else
			{
				txtPop3ConfirmPassword.BackColor = SystemColors.Window;
			}

			int popPort = -1;
			if ((string.IsNullOrEmpty(txtPop3Port.Text)) || (!Int32.TryParse(txtPop3Port.Text,out popPort)) || (popPort < 1) || (popPort > 65535))
			{
				txtPop3Port.BackColor = Color.Tomato;
			}
			else
			{
				txtPop3Port.BackColor = SystemColors.Window;
			}

			//txtSmtpConfirmPassword.Text = inMyAccount.SmtpAccount.Password;
			//txtSmtpAddress.Text = inMyAccount.SmtpAccount.Server;
			//txtSmtpPort.Text = inMyAccount.SmtpAccount.Port.ToString();
			//txtSmtpEmailAddress.Text = inMyAccount.EmailAddress;

			//if (txtSmtpPassword.Text != txtSmtpConfirmPassword.Text)
			//{
			//    txtSmtpConfirmPassword.BackColor = Color.Tomato;
			//}
			//else
			//{
			//    txtSmtpConfirmPassword.BackColor = SystemColors.Window;
			//}

			int smtpPort = -1;
			if ((string.IsNullOrEmpty(txtSmtpPort.Text)) || (!Int32.TryParse(txtSmtpPort.Text, out smtpPort)) || (smtpPort < 1) || (smtpPort > 65535))
			{
				txtSmtpPort.BackColor = Color.Tomato;
			}
			else
			{
				txtSmtpPort.BackColor = SystemColors.Window;
			}
		}

		private void FormControl_ValueChanged(object sender, EventArgs e)
		{
			validateValues();
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			validateValues();

			if (allValuesOk(this.Controls))
			{
				myAccount.Pop3Account.Username = txtPop3Username.Text;
				myAccount.Pop3Account.Password = txtPop3Password.Text;
				myAccount.Pop3Account.Server = txtPop3Address.Text;
				myAccount.Pop3Account.Port = Int32.Parse(txtPop3Port.Text);
				myAccount.Pop3Account.UseAPOP = chkPop3APOP.Checked;
				myAccount.Pop3Account.LeaveMailOnServer = chkPop3LeaveMessages.Checked;
				myAccount.Pop3Account.CheckServerCapabilities = chkPop3CheckCapabilities.Checked;

				if (optPop3EncryptionExplicit.Checked)
				{
					myAccount.Pop3Account.Encryption = EncryptionType.Explicit;
				}
				else if (optPop3EncryptionImplicit.Checked)
				{
					myAccount.Pop3Account.Encryption = EncryptionType.Implicit;
				}
				else
				{
					myAccount.Pop3Account.Encryption = EncryptionType.None;
				}

				if (optSmtpEncryptionExplicit.Checked)
				{
					myAccount.SmtpAccount.Encryption = EncryptionType.Explicit;
				}
				else if (optSmtpEncryptionImplicit.Checked)
				{
					myAccount.SmtpAccount.Encryption = EncryptionType.Implicit;
				}
				else
				{
					myAccount.SmtpAccount.Encryption = EncryptionType.None;
				}

				if (optSmtpEmailSpecific.Checked)
				{
					myAccount.SmtpAccount.ToFilter = EmailToFilter.SpecificAddress;
					myAccount.SmtpAccount.EmailAddress = txtSmtpEmailAddressSpecific.Text;
					myAccount.SmtpAccount.DomainFilter = "";
				}
				else if (optSmtpEmailToHeaders.Checked)
				{
					myAccount.SmtpAccount.EmailAddress = "";

					if (chkSmtpEmailDomainFilter.Checked)
					{
						myAccount.SmtpAccount.ToFilter = EmailToFilter.FilterDomain;
						myAccount.SmtpAccount.DomainFilter = txtSmtpEmailAddressDomainFilter.Text;
					}
					else
					{
						myAccount.SmtpAccount.ToFilter = EmailToFilter.AllToHeaders;
						myAccount.SmtpAccount.DomainFilter = "";
					}
				}

				myAccount.SmtpAccount.Username = txtSmtpUsername.Text;
				myAccount.SmtpAccount.Password = txtSmtpPassword.Text;
				myAccount.SmtpAccount.Server = txtSmtpAddress.Text;
				myAccount.SmtpAccount.Port = Int32.Parse(txtSmtpPort.Text);

				this.DialogResult = DialogResult.OK;
				this.Close();
			}
			else
			{
				MessageBox.Show("There are some values that are invalid.\r\nThese are marked red.\r\n\r\nEither correct these values and try again, or press cancel.", "Invalid Values", MessageBoxButtons.OK, MessageBoxIcon.Error); 
			}
		}

		private bool allValuesOk(Control.ControlCollection inControls)
		{
			bool isOk = true;

			foreach (Control thisControl in inControls)
			{
				isOk = isOk & (thisControl.BackColor != Color.Tomato);

				if (thisControl.Controls.Count > 0)
				{
					isOk = isOk & allValuesOk(thisControl.Controls);
				}
			}

			return isOk;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}

		private void chkPop3SSL_CheckedChanged(object sender, EventArgs e)
		{
			if ((optPop3EncryptionImplicit.Checked) && (txtPop3Port.Text == "110"))
			{
				txtPop3Port.Text = "995";
			}
			else if (((optPop3EncryptionExplicit.Checked) || (optPop3EncryptionNone.Checked)) && (txtPop3Port.Text == "995"))
			{
				txtPop3Port.Text = "110";
			}
		}

		private void chkSmtpSSL_CheckedChanged(object sender, EventArgs e)
		{
			if ((optSmtpEncryptionImplicit.Checked) && (txtSmtpPort.Text == "25"))
			{
				txtSmtpPort.Text = "465";
			}
			else if (((optSmtpEncryptionExplicit.Checked) || (optSmtpEncryptionNone.Checked)) && (txtSmtpPort.Text == "465"))
			{
				txtSmtpPort.Text = "25";
			}
		}

		private void optSmtpEmail_CheckedChanged(object sender, EventArgs e)
		{
			validateValues();

			if (optSmtpEmailSpecific.Checked)
			{
				chkSmtpEmailDomainFilter.Enabled = false;
				txtSmtpEmailAddressDomainFilter.Enabled = false;
				txtSmtpEmailAddressSpecific.Enabled = true;
			}

			if (optSmtpEmailToHeaders.Checked)
			{
				chkSmtpEmailDomainFilter.Enabled = true;
				txtSmtpEmailAddressDomainFilter.Enabled = false;
				txtSmtpEmailAddressSpecific.Enabled = false;

				if (chkSmtpEmailDomainFilter.Checked)
				{
					txtSmtpEmailAddressDomainFilter.Enabled = true;
				}
			}
		}
	}
}
